package staticFlies.test;

import java.io.*;

import cn.ducis.macaw.core.staticflies.file.FileParse;
import cn.ducis.macaw.core.util.random.MRandom;
import cn.ducis.macaw.util.types.StringUtil;

/**
 * 小文件生成器<br>
 * 用于生成大量小文件以测试性能
 * 
 * @author Yellow
 *
 */
public class AtomFileGenerator {
	private static String[] names;
	private static String dir;
	
	public static void main(String[] args) throws IOException{
		BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
		
		System.out.println("输入保存小文件的目录：----------------------------|");
		dir = stdIn.readLine();
		System.out.println("输入生成的文件数量：-----------------------------|");
		int fileNum = stdIn.read();
		System.out.println("输入生成的文件的最大大小（Byte为单位）：---------------|");
		int maxSize = stdIn.read() / 2;
		
		System.out.println("文件生成中...");
		generateFile(fileNum, maxSize);
		System.out.println("生成完毕");
	}
	
	public static void generateFile(int fileNum, int maxSize) {
		MRandom random = new MRandom();
		gernerateName(fileNum);
		for(int i = 0; i < fileNum; i++) {
			String str = random.nextString(maxSize);			
			FileParse.writeFile(str.getBytes(), dir + "/" + names[i]);
		}
	}

	private static void gernerateName(int count) {
		String[] temp = new String[count];
		String[] suffix = {".js",".css"};
		for(int i = 0; i < count; i++) {
			if(i % 2 == 0) {
				temp[i] = StringUtil.generateRandomString(9) + suffix[0];
			}else {
				temp[i] = StringUtil.generateRandomString(9) + suffix[1];
			}
		}
		setDstPath(temp);
	}
	
	public static String[] getDstPath() {
		return names;
	}

	public static void setDstPath(String[] dstPath) {
		AtomFileGenerator.names = dstPath;
	}


	
	
}
